const express = require("express");
// 引入连接池模块
const pool = require("../pool.js");
// 创建路由器对象
const r = express.Router();

// 查找发现内容
r.get("/search/:uid",(req,res)=>{
  var $uid = req.query.uid;
  var sql = "select * from bo_user where uid = ?";
  pool.query(sql,[$uid],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send(result)
		}else{
			res.send("0")
    }
  })
});
//登录
r.post("/login",(req,res)=>{
  var obj = req.body
  var sql = `select * from bo_user where uname = ? and upwd = ?`;
  pool.query(sql,[obj.uname,obj.upwd],(err,result)=>{
    if(err) throw err;
    if(result.length > 0){
      res.send(result)
      console.log(obj)
    }else{
      res.send("0")
    }
  })
});

r.post("/res",(req,res)=>{
  var obj = req.body
  var sql = "insert into bo_user set?"
  pool.query(sql,[obj],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows > 0){
      res.send(result)
      console.log(obj)
    }else{
      res.send("0")
    }
  })
});


// 修改
r.post("/update",(req,res)=>{
  var obj = req.body;
  var sql = "update bo_user set? where uid=?";
  pool.query(sql,[obj,obj.uid],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows > 0){
      res.send(result)
      console.log(obj,obj.uid)
    }else{
      res.send("0")
    }
  })
})

// 导出连接池对象
module.exports = r;